//
//  NearFunVc.m
//  LoveTourism
//
//  Created by qianfeng0 on 16/6/25.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "NearFunVc.h"
#import <AFNetworking.h>
#import "Helper.h"
#import "NearFunModel.h"
#import "NearFunCell.h"
#import "NearFunDetailsVC.h"
#import "AdvertisingModel.h"
#import <UIImageView+WebCache.h>

@interface NearFunVc ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray * dataArray;

//滚动视图的图片数组
@property (nonatomic,strong) NSMutableArray * scrollArray;
@end

@implementation NearFunVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self getData];
}
#pragma  mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (NSMutableArray *)scrollArray {
    if (_scrollArray == nil) {
        _scrollArray = [[NSMutableArray alloc] init];
    }
    return _scrollArray;
}
#pragma mark - 初始化UI
- (void)initUI {
    
    
    //去除tableView和上方之间空白
    self.automaticallyAdjustsScrollViewInsets = NO;
    //隐藏滚动视图的滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    //注册cell
    UINib * nib = [UINib nibWithNibName:@"NearFunCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"NearFunCell"];
    
    
}

#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.
    static NSString * identifier = @"NearFunCell";
    //2.
    NearFunCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    cell.model = self.dataArray[indexPath.row];
    
    //4.
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NearFunModel * model = self.dataArray[indexPath.row];
    
    NearFunDetailsVC * vc = [[NearFunDetailsVC alloc] initWithNibName:@"NearFunDetailsVC" bundle:nil];
    vc.str = [NSString stringWithFormat:NEARFUNDETAILS_URL,model.ProductId];
    
    //隐藏分栏
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 190;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //实现给cell添加一个3D动画，
    //设置x,y的初始值为0.1，z的初始值为1
    //一般的动画效果都是在layer层实现的
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    [UIView animateWithDuration:0.5 animations:^{
        //设置cell 的x和y的最终值为1
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
        
    }];
}
#pragma mark - 获取数据
- (void)getData {
    
    AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
    
    NSString * url = NEARFUN_URL;
    
    [mgr GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * dict = responseObject[@"Response"][@"RecommendList"];
        
        NSDictionary * dic = responseObject[@"Response"][@"RecommendTourList"];
        NSArray * array = [NearFunModel arrayOfModelsFromDictionaries:dict[@"ProductItem"] error:nil];
        NSArray * marray = [NearFunModel arrayOfModelsFromDictionaries:dic[@"ProductItem"] error:nil];
        
        //添加到数据源数组中
        [self.dataArray addObjectsFromArray:array];
        [self.dataArray addObjectsFromArray:marray];
        
        [_tableView reloadData];
        
        //滚动视图的图片数组
        NSDictionary * dict2 = responseObject[@"Response"];
        NSArray * array2 = [AdvertisingModel arrayOfModelsFromDictionaries:dict2[@"HotDestinationByBanner"] error:nil];
        
        self.scrollArray.array = array2;
        //添加imageView
        [self addimageView];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}
#pragma mark - 添加滚动视图
- (void)addimageView {
    
    for (int i = 0; i < self.scrollArray.count; i++) {
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth / 4 * i, 0, ScreenWidth / 4, 70)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[_scrollArray[i] ImgSrc]] placeholderImage:[UIImage imageNamed:@"placeholder@2x"]];
        //添加手势
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(go:)];
        imageView.tag = 100 + i;
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        
        
        [_scrollView addSubview:imageView];
    }
    //设置滚动范围
    _scrollView.contentSize = CGSizeMake(self.scrollArray.count * self.view.frame.size.width / 4, 70);
}
- (void)go:(UITapGestureRecognizer *)tap {
    
    NearFunDetailsVC * vc = [[NearFunDetailsVC alloc] initWithNibName:@"NearFunDetailsVC" bundle:nil];
    vc.str = [self.scrollArray[tap.view.tag - 100] Link];
    
    
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)pop:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end


































